package com.hunt.service.impl;

import com.hunt.dao.OnlineStatisticsTaskMapper;
import com.hunt.model.dto.HisStatisUsersOnline;
import com.hunt.model.dto.UserInfo;
import com.hunt.model.entity.ThisTimeOnlineStatistics;
import com.hunt.service.OnlineStatisticsTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ww
 * @className
 * @description
 * @date 2020/6/22
 */
@Service
@Transactional
public class OnlineStatisticsTaskServiceImpl implements OnlineStatisticsTaskService {
    
    
    private static final Logger log = LoggerFactory.getLogger(OnlineStatisticsTaskServiceImpl.class);
    
    @Autowired
    private OnlineStatisticsTaskMapper onlineStatisticsTaskMapper;
    
    @Override
    public void getAllOnlineUser() {
        //查询所有在线用户(UserInfo) 没有在线用户则更新三个统计表 并删除临时表中今天之前的数据
        List<UserInfo> allOnlineUser = onlineStatisticsTaskMapper.getAllOnlineUser();
        if (null == allOnlineUser || allOnlineUser.size() == 0){
            setCount2OnlineStatis();
            return;
        }
        //查询临时表(onlineStatistics)中今天已经存在的用户
        List<String> countedUserIdList = onlineStatisticsTaskMapper.getUserFromTemporaryTable();
        
        if (null != countedUserIdList && countedUserIdList.size() > 0){
            //循环所有当前在线用户  如果临时表中包含就跳出本次循环
            for (UserInfo userInfo : allOnlineUser) {
                if (countedUserIdList.contains(getTodayDateStr(userInfo.getUserId()))){
                    continue;
                }else{
                    saveNewOnlineUser(userInfo);
                }
            }
        }else{
            for (UserInfo userInfo : allOnlineUser) {
                saveNewOnlineUser(userInfo);
            }
        }
        setCount2OnlineStatis();
    }

    /**
     * 存储新的用户到在线统计临时表中(onlineStatisticsTab)
     */
    private void saveNewOnlineUser(UserInfo userInfo) {
        try {
            userInfo.setId(getTodayDateStr(userInfo.getUserId()));
            userInfo.setOnlineDateTime(new Date());
            onlineStatisticsTaskMapper.saveOnlineUser(userInfo);
        }catch (Exception e){
            log.debug("Update onlineStatisticsTask Table , The primary key repeat");
        }
    }

    /**
     *  将本次定时统计的数量更新到各自统计表中
     */
    private void setCount2OnlineStatis(){
        List<ThisTimeOnlineStatistics> thisTimeOnlineStatisticsList = onlineStatisticsTaskMapper.getTodayOnlineCount();
        HisStatisUsersOnline hisStatisUsersOnline = new HisStatisUsersOnline();
        for (ThisTimeOnlineStatistics thisTimeOnlineStatistics : thisTimeOnlineStatisticsList) {
            try {
                if (thisTimeOnlineStatistics.getUserLevel().equals("0")){
                    hisStatisUsersOnline.setId(getTodayDateStr());
                    hisStatisUsersOnline.setNumbers(thisTimeOnlineStatistics.getNumbers());
                    hisStatisUsersOnline.setDate(new Date());
                    onlineStatisticsTaskMapper.setTmpTodayCount(hisStatisUsersOnline);
                    continue;
                }
                if (thisTimeOnlineStatistics.getUserLevel().equals("1")){
                    hisStatisUsersOnline.setId(getTodayDateStr());
                    hisStatisUsersOnline.setNumbers(thisTimeOnlineStatistics.getNumbers());
                    hisStatisUsersOnline.setDate(new Date());
                    onlineStatisticsTaskMapper.setRentTodayCount(hisStatisUsersOnline);
                    continue;
                }
                if (thisTimeOnlineStatistics.getUserLevel().equals("2")){
                    hisStatisUsersOnline.setId(getTodayDateStr());
                    hisStatisUsersOnline.setNumbers(thisTimeOnlineStatistics.getNumbers());
                    hisStatisUsersOnline.setDate(new Date());
                    onlineStatisticsTaskMapper.setFormalTodayCount(hisStatisUsersOnline);
                    continue;
                }
                if (thisTimeOnlineStatistics.getUserLevel().equals("3")){
                    hisStatisUsersOnline.setId(getTodayDateStr());
                    hisStatisUsersOnline.setNumbers(thisTimeOnlineStatistics.getNumbers());
                    hisStatisUsersOnline.setDate(new Date());
                    onlineStatisticsTaskMapper.setVIPTodayCount(hisStatisUsersOnline);
                    continue;
                }
                if (thisTimeOnlineStatistics.getUserLevel().equals("4")){
                    hisStatisUsersOnline.setId(getTodayDateStr());
                    hisStatisUsersOnline.setNumbers(thisTimeOnlineStatistics.getNumbers());
                    hisStatisUsersOnline.setDate(new Date());
                    onlineStatisticsTaskMapper.setNormalTodayCount(hisStatisUsersOnline);
                }
            }catch (Exception e){
                log.debug("Save to his_statis_users_online table, The primary key repeat");
                e.printStackTrace();
            }
        }
        onlineStatisticsTaskMapper.deleteOldOnlineStatisticsData();
    }
    
    

    private synchronized String getTodayDateStr(String userId){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(new Date()) + userId;
    }

    private synchronized String getTodayDateStr(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(new Date());
    }
    
}
